package com.lb.videoplay.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.IntentUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lb.videoplay.R;
import com.lb.videoplay.activity.DetailsVideoActivity;
import com.lb.videoplay.adapter.AssortAdapter;
import com.lb.videoplay.model.AssortModel;
import com.lb.videoplay.utils.GridSpacingItemDecoration;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Mr.lb
 * 时间： 2018/11/15
 * 包名： com.lb.videoplay.activity
 * 描述： 分类 Fragment
 */
public class AssortFragment extends Fragment {

    private RecyclerView mRecyclerView;
    private List<AssortModel> mAssortModels;
    private AssortAdapter mAssortAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //通过参数中的布局填充获取对应布局
        View view = inflater.inflate(R.layout.fragment_assort, container, false);
        initWidget(view);
        initData();
        return view;
    }

    //初始化控件
    private void initWidget(View view) {
        mRecyclerView = view.findViewById(R.id.rv_assort);
    }

    //初始化数据
    private void initData() {
        mAssortModels = new ArrayList<>();

        AssortModel assortModel = new AssortModel();
        assortModel.setType(1);
        assortModel.setType_name("广告");
        assortModel.setPic(R.drawable.assort_0);
        mAssortModels.add(assortModel);

        AssortModel assortModel1 = new AssortModel();
        assortModel1.setType(2);
        assortModel1.setType_name("生活");
        assortModel1.setPic(R.drawable.assort_1);
        mAssortModels.add(assortModel1);

        AssortModel assortModel2 = new AssortModel();
        assortModel2.setType(3);
        assortModel2.setType_name("动画");
        assortModel2.setPic(R.drawable.assort_2);
        mAssortModels.add(assortModel2);

        AssortModel assortModel3 = new AssortModel();
        assortModel3.setType(4);
        assortModel3.setType_name("搞笑");
        assortModel3.setPic(R.drawable.assort_3);
        mAssortModels.add(assortModel3);

        AssortModel assortModel4 = new AssortModel();
        assortModel4.setType(5);
        assortModel4.setType_name("创意");
        assortModel4.setPic(R.drawable.assort_4);
        mAssortModels.add(assortModel4);

        AssortModel assortModel5 = new AssortModel();
        assortModel5.setType(6);
        assortModel5.setType_name("运动");
        assortModel5.setPic(R.drawable.assort_5);
        mAssortModels.add(assortModel5);

        AssortModel assortModel6 = new AssortModel();
        assortModel6.setType(7);
        assortModel6.setType_name("音乐");
        assortModel6.setPic(R.drawable.assort_6);
        mAssortModels.add(assortModel6);

        AssortModel assortModel7 = new AssortModel();
        assortModel7.setType(8);
        assortModel7.setType_name("旅行");
        assortModel7.setPic(R.drawable.assort_7);
        mAssortModels.add(assortModel7);

        AssortModel assortModel8 = new AssortModel();
        assortModel8.setType(9);
        assortModel8.setType_name("影视");
        assortModel8.setPic(R.drawable.assort_8);
        mAssortModels.add(assortModel8);

        AssortModel assortModel9 = new AssortModel();
        assortModel9.setType(10);
        assortModel9.setType_name("游戏");
        assortModel9.setPic(R.drawable.assort_9);
        mAssortModels.add(assortModel9);

        AssortModel assortModel10 = new AssortModel();
        assortModel10.setType(11);
        assortModel10.setType_name("综艺");
        assortModel10.setPic(R.drawable.assort_10);
        mAssortModels.add(assortModel10);

        mAssortAdapter = new AssortAdapter(getActivity(), R.layout.item_assort, mAssortModels);
        int spanCount = 2; // 3 columns
        int spacing = 80; // 50px
        mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, false));
        //设置九宫格布局
        GridLayoutManager gs = new GridLayoutManager(getActivity(), spanCount);
        mRecyclerView.setLayoutManager(gs);
        mRecyclerView.setAdapter(mAssortAdapter);

        mAssortAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                AssortModel model = (AssortModel) adapter.getData().get(position);
                Intent intent = new Intent(getActivity(), DetailsVideoActivity.class);
                intent.putExtra("type", model.getType_name());
                startActivity(intent);
            }
        });
    }
}
